﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Streaming_Service.User;
using System.Data;
using Streaming_Service.Media;

namespace Streaming_Service.SQL_Ting {
    public class PricingQueries {
        public static int BasicHdPrice = 15;
        public static int BasicSdPrice = 10;

        public static decimal GetHdPrice(MovieEntry movie, SqlHandler sh) {
            return GetHdPrice(movie.Id, sh);
        }

        public static decimal GetSdPrice(MovieEntry movie, SqlHandler sh) {
            return GetSdPrice(movie.Id, sh);
        }

        public static decimal GetHdPrice(int movieId, SqlHandler sh) {
            var SQL = "SELECT releasedate FROM movie WHERE id = @id";

            var c = sh.CreateCommand(SQL);
            c.Parameters.Add("id", SqlDbType.Int).Value = movieId;

            var mEntry = new MovieEntry();

            var r = c.ExecuteReader();
            while(r.Read()) {
                mEntry.ReleaseDate = r.GetDateTime(r.GetOrdinal("releasedate"));
            }
            r.Close();

            return GetHdPrice(mEntry.ReleaseDate);
        }

        public static decimal GetSdPrice(int movieId, SqlHandler sh) {
            var SQL = "SELECT releasedate FROM movie WHERE id = @id";

            var c = sh.CreateCommand(SQL);
            c.Parameters.Add("id", SqlDbType.Int).Value = movieId;

            var mEntry = new MovieEntry();

            var r = c.ExecuteReader();
            while(r.Read()) {
                mEntry.ReleaseDate = r.GetDateTime(r.GetOrdinal("releasedate"));
            }
            r.Close();

            return GetSdPrice(mEntry.ReleaseDate);
        }

        public static decimal GetSdPrice(DateTime? releaseDate) {
            if(releaseDate == null) {
                return 0;
            }

            var diff = (DateTime.Now - (DateTime)releaseDate);
            var months = diff.TotalDays / 30;
            var monthlyrate = 0.02;
            var takeoff = months * monthlyrate;
            var price = BasicSdPrice - takeoff;
            return (decimal)Math.Round(price, 2);
        }

        public static decimal GetHdPrice(DateTime? releaseDate) {
            if(releaseDate == null) {
                return 0;
            }

            var diff = (DateTime.Now - (DateTime)releaseDate);
            var months = diff.TotalDays / 30;
            var monthlyrate = 0.02;
            var takeoff = months * monthlyrate;
            var price = BasicHdPrice - takeoff;
            return (decimal)Math.Round(price, 2);
        }
    }
}